<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_sku_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $pid = isset($_GET['pid']) ? $_GET['pid'] : '0';
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $cats_query = $this->db->query("select * from " . table("category"));
        while ($cats_row = $this->db->fetch_array($cats_query)) {
            $gcats[$cats_row['cid']] = $cats_row['name'];
        }
        $query = $this->db->get_all("select * from " . table('goods_sku') . " where pid = " . $pid . " order by px desc");
        if ($query) {
            foreach ($query as $k => $v) {
                if ($v['cid']) {
                    $v['gcname'] = $gcats[$v['cid']];
                } else {
                    $v['gcname'] = '未分组';
                }
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('goods_sku') . " where pid = " . $pid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function edit()
    {
        $id = !empty($_GET['id']) ? $_GET['id'] : '';
        if ($id) {
            $row = $this->module('goods_sku')->get_one($id);
            if ($row['cid'] != 0) {
                $cats = $this->module('category')->get_parents_cid($row['cid']);
                $this->assign('update_cats', $cats);
            }
            $this->assign('row', $row);
        }
        $this->display('goods_sku/edit');
    }
    public function save()
    {
        if ($_POST) {
            $pid = !empty($_POST['pid']) ? $_POST['pid'] : '0';
            if ($pid) {
                $data['cid'] = '0';
                $data['pid'] = $pid;
            } else {
                $data['cid'] = isset($_POST['cid']) ? $_POST['cid'] : '0';
                $data['pid'] = '0';
            }
            $data['px'] = !empty($_POST['px']) ? $_POST['px'] : '0';
            $id = isset($_POST['id']) ? $_POST['id'] : false;
            $name = !empty($_POST['name']) ? $_POST['name'] : error_json('名称不能为空');
            if ($id) {
                $data['name'] = $name;
                $query = $this->db->update(table('goods_sku'), $data, 'id = ' . $id);
            } else {
                $name_arr = explode(',', $name);
                $count = 0;
                $loop = 0;
                $this->db->autocommit();
                foreach ($name_arr as $v) {
                    $data['name'] = $v;
                    $exec = $this->db->add(table('goods_sku'), $data);
                    if ($exec) {
                        $count++;
                    }
                    $loop++;
                }
                if ($count == $loop && $count > '0') {
                    $this->db->commit();
                    $query = true;
                } else {
                    $this->db->rollback();
                    $query = false;
                }
            }
            if ($query) {
                right_json();
            } else {
                error_json('修改失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function del()
    {
        $id = P('id', '提交编号错误');
        $query = $this->db->delete(table('goods_sku'), 'id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
}